package com.isoft.service.flight.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.dao.flight.FlightViewDao;
import com.isoft.entity.ball.Blls;
import com.isoft.entity.flight.FlightView;
import com.isoft.service.flight.FlightViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 航班视图服务层实现类
 */
@Service
public class FlightViewServiceImpl implements FlightViewService {
    @Autowired
    FlightViewDao flightViewDao;

    //查询所有航班信息
    @Override
    public List<FlightView> selectAllFlightView() {
       return flightViewDao.selectAllFlightView();
    }

    //根据航班号查询航班信息
    @Override
    public List<FlightView> selectFlightViewByFLID(Integer FLID) {
        return flightViewDao.selectFlightViewByFLID(FLID);
    }
    //增加航班信息
    @Override
    public Boolean insertFlightView(FlightView flightView) {

        List<FlightView> all = flightViewDao.selectAllFlightView();
//        如果FLID存在就返回false
        for (FlightView view : all) {
            if (flightView.getFLID().equals(view.getFLID()))
                return false;
        }
        return flightViewDao.insertFlightView(flightView) > 0;
    }
    //修改航班信息
    @Override
    public Boolean updateFlightView(FlightView flightView) {
        return flightViewDao.updateFlightView(flightView) > 0;
    }

    //删除航班信息
    @Override
    public Boolean deleteFlightView(Integer ID) {
        return flightViewDao.deleteFlightView(ID) > 0;
    }


    /**
     * 分页查询
     */
    public  IPage<FlightView> getFlighViewtPage(int currentPage, int pageSize) {
        try {
            // 构建分页对象
            Page<FlightView> page = new Page<>(currentPage, pageSize);

            LambdaQueryWrapper<FlightView> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(FlightView::getFIDE,FlightView::getID,FlightView::getFLID,FlightView::getFRLT,FlightView::getFRTT,FlightView::getVALID);
            queryWrapper.orderByDesc(FlightView::getID); // 按ID降序排序
            Page<FlightView> flightViewPage = flightViewDao.selectPage(page,queryWrapper);
            // 封装返回
            return flightViewPage;
        } catch (Exception e) {
            // 异常处理逻辑，此处可根据需要抛出异常或处理后返回默认值
            throw new RuntimeException("获取数据失败：" + e.getMessage());
        }
    }
}
